<?php

/*
 * Marker generator
 */
define('ROOT_DIR', __DIR__.DIRECTORY_SEPARATOR);
define('MARKERS_DIR', ROOT_DIR.'markers'.DIRECTORY_SEPARATOR);
define('PROTOTYPE_MARKERS_DIR', ROOT_DIR.'images'.DIRECTORY_SEPARATOR.'map-icons'.DIRECTORY_SEPARATOR);
define('FONTS_DIR', ROOT_DIR.'fonts'.DIRECTORY_SEPARATOR);


define('FONT_FILE', FONTS_DIR.'arial.ttf');
define('FONT_SIZE',16);
define('FONT_COLOR', '#FFFFFF');


abstract class Marker{
    protected $__text=1;
    public function __construct($text) {
        $this->__text = $text;
    }
    public function ImagePath(){
        return PROTOTYPE_MARKERS_DIR.'empty.png';
    }
    public function fontFile(){
        return FONT_FILE;
    }
    public function fontColor(){
        return FONT_COLOR;
    }
    public function fontSize(){
        return FONT_SIZE;
    }
    public function gravity(){
        return Imagick::GRAVITY_CENTER;
    }
    public function alpha(){
        return 1;
    }
    public function posX(){
        return 0;
    }
    public function posY(){
        return 0;
    }
    public function rotateAngle(){
        return 0;
    }
    public function text(){
        return $this->__text;
    }    
}
class EmptyMarker extends Marker{
    public function fontSize(){
        if(100>$this->text())
            return FONT_SIZE;
        else if(1000 > $this->text())
            return FONT_SIZE - 2;
        else return    FONT_SIZE - 4;
    }
    public function posY(){
        return -5;
    }
}
class ZoomStackMarker extends Marker{
    public function ImagePath(){
        return PROTOTYPE_MARKERS_DIR.'zoom_stack.png';
    }
    public function fontColor() {
        return '#999999';
    }    
}
class BaseMarker extends Marker{
    public function ImagePath(){
        return PROTOTYPE_MARKERS_DIR.'base.png';
    }
    public function fontSize(){
        if(100>$this->text())
            return FONT_SIZE;
        else if(1000 > $this->text())
            return FONT_SIZE - 2;
        else return    FONT_SIZE - 4;
    }
    public function posY(){
        return -3;
    }
    public function fontFile(){
        return FONTS_DIR.'arialbd.ttf';
    }
}
class MarkerBuilder{
    protected $__marker;
    protected $imagick;
    public function __construct() {
        $this->__marker = $marker;              
    }
    protected function getImagick(){
        if(!$this->imagick)
            $this->imagick = new Imagick();
        return $this->imagick;
    }
    
    public function generateMarker(Marker $marker){
        $im = $this->getImagick();
        
       /*** check image file ***/
        if(!file_exists($marker->ImagePath()))
            throw new Exception('file not found');
        
         /*** read the image into the object ***/
        $im->readImage( $marker->ImagePath() );

        /* Create a drawing object and set the font size */
        $draw = new ImagickDraw();

        /*** set the font ***/
        $draw->setFont( $marker->fontFile() );

        /*** set the font size ***/
        $draw->setFontSize( $marker->fontSize() );
        $draw->setFillColor( new ImagickPixel( $marker->fontColor()) );
        /*** add some transparency ***/
        $draw->setFillAlpha( $marker->alpha() );

        /*** set gravity to the center ***/
        $draw->setGravity( $marker->gravity() );

        /*** overlay the text on the image ***/
        $im->annotateImage( 
                $draw, $marker->posX(), 
                $marker->posY(), 
                $marker->rotateAngle(), 
                $marker->text() 
        );
        /**** set to png ***/
        $im->setImageFormat( "png" );
    }
    
    public function saveFile($fileName){
        $this->getImagick()->writeImage($fileName);
    }
    public function getImageBlob(){
        return $this->getImagick()->getimageblob();
    }
}


try{
    $name = $_GET['name'];
    $count = (int) $_GET['count'];
    $image = PROTOTYPE_MARKERS_DIR.$name.'.png';
//    if(!file_exists($image))
//        throw new Exception('image not found');
    
    $newMarkerDIR = MARKERS_DIR.$name;
    if(!is_dir($newMarkerDIR))
        mkdir ($newMarkerDIR, 0777, true);
    
    $newImagePath = $newMarkerDIR.DIRECTORY_SEPARATOR.$count.'.png';
    if(!$count) 
        throw new Exception('count is not set');
    
    switch ($name){
        case 'zoom':
            $marker = new ZoomStackMarker($count);
            break;
        case 'base':
            $marker = new BaseMarker($count);
            break;
        default :
            throw new Exception('not valid marker type');
    }
    $markerBuilder = new MarkerBuilder();
    $markerBuilder->generateMarker($marker);
    $markerBuilder->saveFile($newImagePath);
    header("Content-Type: image/png");
    echo $markerBuilder->getImageBlob();
    
}
catch(Exception $e)
{
    echo $e->getMessage();
}


//print_r($_GET);



?>